import React from 'react';
import { inject, observer } from 'mobx-react';
import * as mobx from 'mobx';
import base from './../../../util/base';
import Content from './Content'
import { getAntd, getEcCom, getLabel } from '../../../util/convert';

@inject('freeWflowSimpleStore')
@observer
export default class Main extends React.Component {

    componentWillMount() {
        const { loadCondition, synchronousRemarkData } = this.props.freeWflowSimpleStore;
        synchronousRemarkData();
        setTimeout(() => {//刚加载表单的时候全量组件库还没加载，此时会报WeaForm取不到的错
            const { commonParam = {} } = base.globalStore;
            const { requestid, nodeid } = commonParam;
            loadCondition({ requestid, nodeid });
        }, 1000)
    }

    render() {
        const { WeaDialog } = getEcCom();
        const { visible = false, freeWflowSimpleStore } = this.props;
        const { onCloseDialog } = freeWflowSimpleStore;
        const { loading } = base.globalStore;

        return (
            <WeaDialog
                visible={visible}
                loading={loading}
                title={getLabel(21781, '流转设定')}
                icon="icon-coms-workflow"
                iconBgcolor='#0079DE'
                onCancel={onCloseDialog}
                loading={loading}
                style={{ width:980, height: 610, padding: "10px 20px" }}
                loadCss={true}
                initLoadCss={true}
                maskClosable={false}
                layout={jQuery('.wf-req')[0]}
                buttons={this.getButtons()}
                moreBtn={this.getMoreBtns()}
                hasScroll
            >
                {visible && <Content visible={visible} getMoreBtns={this.getMoreBtns} />}
            </WeaDialog>
        )
    }

    getButtons = () => {
        const { Button } = getAntd();
        const { doSubmit} = this.props.freeWflowSimpleStore;
        const { btnDisabled } = base.globalStore;
        let buttons = [];
        let rightBtnSubmitArr = this.getRightMenuSubmit()
        if(rightBtnSubmitArr.length > 0){
            rightBtnSubmitArr.map((item,i)=>{
                buttons.push(<Button disabled={btnDisabled} type='primary' onClick={()=>{doSubmit(item.menuFun)}}>{item.menuName}</Button>);
            })
        }
        return buttons;
    }

    getRightMenuSubmit = () =>{
        const {  rightMenu = {} } = base.globalStore;
        const { rightMenus = [] } = rightMenu;
        let rightBtnSubmitArr = [];
        rightMenus.map(item => {
            if (item.systemMenuType == "APPROVE" || item.systemMenuType == "SUBMIT")
                rightBtnSubmitArr.push(item)
        })
        return rightBtnSubmitArr;
    }


    getMoreBtns = () => {
        const { doSubmit} = this.props.freeWflowSimpleStore;
        const { btnDisabled, rightMenu = {} } = base.globalStore;
        const { rightMenus = [] } = rightMenu;
        let btns=[]
        let rightBtnSubmitArr = this.getRightMenuSubmit()
        if(rightBtnSubmitArr.length > 0){
            rightBtnSubmitArr.map((item,i)=>{
                btns.push({
                    key: i+"",
                    icon: <i className='icon-coms-Preservation' />,
                    content: item.menuName,
                    disabled: btnDisabled,
                    onClick: ()=>{doSubmit(item.menuFun)},
                })
            })
        }
        return { datas: btns };
    }
    doSubmit = () => {
        const { doSubmit } = this.props.freeWflowSimpleStore;
        doSubmit();
    }

}
